package main.java.ui;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.bytedeco.javacv.Java2DFrameUtils;
import org.bytedeco.opencv.global.opencv_imgproc;
import org.bytedeco.opencv.opencv_core.Mat;
import org.bytedeco.opencv.opencv_core.Size;

import static org.bytedeco.opencv.global.opencv_imgcodecs.imread;

import main.java.lpr.LPR;
import main.java.lpr.Plate;

/**
 * 窗口
 *
 * @author alvin
 */
public class LPRFrame extends JFrame {

    private LPR lpr = null;

    private Font font = new Font("宋体", Font.BOLD, 30);

    private JLabel jlbImg;

    // 识别结果
    private JTextField car_id_txt_1;
    private JTextField car_id_txt_2;
    private JTextField car_id_txt_3;

    // 车牌截图
    private JLabel car_id_img_1;
    private JLabel car_id_img_2;
    private JLabel car_id_img_3;

    public LPRFrame(LPR lpr) {
        this.lpr = lpr;
        this.setTitle("open-lpr");
        this.setSize(1000, 500);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
        this.validate();
        this.setResizable(false);
        this.setLayout(new BorderLayout());// 边框布局

        // 图片显示区域
        JPanel movieP = new JPanel();
        movieP.setSize(700, 450);
        movieP.setLayout(new BorderLayout());
        jlbImg = new JLabel();// 显示图像
        movieP.add(jlbImg);

        // 车牌显示
        JPanel parkingsP = new JPanel();
        parkingsP.setSize(300, 500);
        parkingsP.setLayout(new GridLayout(4, 2));

        /** 1.第一个车牌 */
        JPanel parkingP_1 = new JPanel();
        parkingP_1.setLayout(new GridLayout(2, 2));// 网格布局
        JLabel id = new JLabel("车牌:");
        parkingP_1.add(id);
        car_id_img_1 = new JLabel();
        parkingP_1.add(car_id_img_1);

        JLabel id3 = new JLabel("识别结果:");
        parkingP_1.add(id3);
        car_id_txt_1 = new JTextField("                                             ");
        car_id_txt_1.setFont(font);
        parkingP_1.add(car_id_txt_1);

        parkingsP.add("Center", parkingP_1);

        /** 2.第2车牌 */
        JPanel parkingP_2 = new JPanel();
        parkingP_2.setLayout(new GridLayout(2, 2));// 网格布局
        JLabel id4 = new JLabel("车牌:");
        parkingP_2.add(id4);
        car_id_img_2 = new JLabel();
        parkingP_2.add(car_id_img_2);

        JLabel id6 = new JLabel("识别结果:");
        parkingP_2.add(id6);
        car_id_txt_2 = new JTextField("                                               ");
        car_id_txt_2.setFont(font);
        parkingP_2.add(car_id_txt_2);

        parkingsP.add("North", parkingP_2);

        /** 3.第3个车牌 */
        JPanel parkingP_3 = new JPanel();
        parkingP_3.setLayout(new GridLayout(2, 2));// 网格布局
        JLabel id74 = new JLabel("车牌:");
        parkingP_3.add(id74);
        car_id_img_3 = new JLabel();
        parkingP_3.add(car_id_img_3);

        JLabel id10 = new JLabel("识别结果:");
        parkingP_3.add(id10);
        car_id_txt_3 = new JTextField("");
        car_id_txt_3.setFont(font);
        parkingP_3.add(car_id_txt_3);

        parkingsP.add("North", parkingP_3);

        /** 5. 选择图片区域 */
        JPanel car_seach = new JPanel();
        car_seach.setLayout(new GridLayout(2, 2));
        car_seach.add(new JPanel());
        car_seach.add(new JPanel());

        // car_seach_id = new JTextField("桂MZE120");
        // car_seach.add(car_seach_id);
        // 搜索按钮
        JButton seach = new JButton("选择图片识别");
        seach.addActionListener(event -> {
            System.out.println("****车牌检测****");
            JFileChooser fc = new JFileChooser();
            fc.setDialogTitle("打开");
            JFileChooser chooser = new JFileChooser();
            FileNameExtensionFilter filter = new FileNameExtensionFilter("jpg与png图片", "jpg", "png");
            fc.setFileFilter(filter);
            int rtn = chooser.showOpenDialog(this);
            // parent为父窗口
            if (rtn == JFileChooser.APPROVE_OPTION) {
                String name = chooser.getSelectedFile().getPath();
                System.out.println("name=" + name);

                findPlates(name);
            }
        });

        car_seach.add(seach);

        parkingsP.add("North", car_seach);

        /**
         * 将视频区域和车位区域添加进入窗口
         */
        this.getContentPane().add("Center", movieP);
        this.getContentPane().add("East", parkingsP);

        // 窗口关闭事件
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }

        });

        // 测试图像
        findPlates("imgs/MZE120.jpg");
    }

    private void findPlates(String name) {
        car_id_img_1.setIcon(new ImageIcon(""));
        car_id_img_2.setIcon(new ImageIcon(""));
        car_id_img_3.setIcon(new ImageIcon(""));
        car_id_txt_1.setText("");
        car_id_txt_2.setText("");
        car_id_txt_3.setText("");
        // 加载图片
        Mat src = imread(name);
        List<Plate> plates = lpr.detection(src, 0.7, 0.7);
        for (int i = 0; i < plates.size(); i++) {
            Plate p = plates.get(i);
            String text = p.getPlateResult().getPlateNumber() + " " + p.getType();
            Mat img = new Mat();
            opencv_imgproc.resize(p.getMat(), img, new Size(164, 48));
            if (i == 0) {
                car_id_img_1.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
                car_id_txt_1.setText(text);
            } else if (i == 1) {
                car_id_img_2.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
                car_id_txt_2.setText(text);
            } else if (i == 2) {
                car_id_img_3.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
                car_id_txt_3.setText(text);
            }

        }

        // 左边图片显示
        int maxW = 650;
        int maxH = 450;
        boolean up = true;
        if (src.arrayHeight() > maxH || src.arrayWidth() > maxW) {
            up = false;
        }

        if (up) {
            // src扩大
            double b1 = (double) maxW / (double) src.arrayWidth();
            double b2 = (double) maxH / (double) src.arrayHeight();
            if (b1 > b2) {
                // 以b2为比例扩大
                Mat img = new Mat();
                opencv_imgproc.resize(src, img,
                        new Size((int) (src.arrayWidth() * b2), (int) (src.arrayHeight() * b2)));
                jlbImg.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
            } else {
                // 以b1为比例扩大
                Mat img = new Mat();
                opencv_imgproc.resize(src, img,
                        new Size((int) (src.arrayWidth() * b1), (int) (src.arrayHeight() * b1)));
                jlbImg.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
            }

        } else {
            // src缩小
            double b1 = (double) src.arrayWidth() / (double) maxW;
            double b2 = (double) src.arrayHeight() / (double) maxH;

            if (b1 > b2) {
                // 以b1为比例缩小
                Mat img = new Mat();
                opencv_imgproc.resize(src, img,
                        new Size((int) (src.arrayWidth() / b1), (int) (src.arrayHeight() / b1)));
                jlbImg.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
            } else {
                // 以b2为比例缩小
                Mat img = new Mat();
                opencv_imgproc.resize(src, img,
                        new Size((int) (src.arrayWidth() / b2), (int) (src.arrayHeight() / b2)));
                jlbImg.setIcon(new ImageIcon(Java2DFrameUtils.toBufferedImage(img)));
            }
        }
    }

    public void showImg(BufferedImage src) {
        jlbImg.setIcon(new ImageIcon(src));
    }

    public void setParking_1_carID(String carId) {
        if (car_id_txt_1 != null) {
            car_id_txt_1.setText(carId);
        }
    }

    public void setParking_2_carID(String carId) {
        if (car_id_txt_2 != null) {
            car_id_txt_2.setText(carId);
        }
    }

    public void setParking_3_carID(String carId) {
        if (car_id_txt_3 != null) {
            car_id_txt_3.setText(carId);
        }

    }

    public void setParking_1_parking(String parkingId) {
        if (car_id_img_1 != null) {
            car_id_img_1.setText(parkingId + "                                                                      ");
        }

    }

    public void setParking_2_parking(String parkingId) {
        if (car_id_img_2 != null) {
            car_id_img_2.setText(parkingId + "                                                                      ");
        }

    }

    public void setParking_3_parking(String parkingId) {
        if (car_id_img_3 != null) {
            car_id_img_3.setText(parkingId + "                                                                      ");
        }

    }

}
